<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        //js创建链表
        class Node{
            constructor(element){
                this.item = element;//数据
                this.next = null;//指针：下一节点的位置
            }
        }
        class LinkedList{
            constructor(){
                this.head = null;
                this.length  = 0;
            }
            // 向链表尾部追加元素
            append(element){
                let node = new Node(element)
                if(this.head === null){
                    this.head = node;
                }else{
                    let tail = this.head;
                    while(tail.next){
                        tail = tail.next;
                    }
                    tail.next = node
                }
                this.length+=1
            }
        }
        const l1 = new LinkedList()
        l1.append(1)
        l1.append(2)
        l1.append(4)
        const l2 = new LinkedList()
        l2.append(1)
        l2.append(3)
        l2.append(4)

        // var l1 = [1,2,4];
        // var l2 = [1,3,4];
        var mergeTwoLists = function(l1, l2) {
           
            var curr = new LinkedList()
            var dummy = curr;
            head1 = l1.head;
            head2=l2.head;
            while(head1!==null&&head2!==null){
                if(head1.item<head2.item){
                    curr.next = head1;
                    head1= head1.next;
                }else{
                    curr.next = head2;
                    head2 =  head2.next;
                }
                curr = curr.next;
            }
            if(head1!==null){
                curr.next = head1
            }
            if(head2!==null){
                curr.next = head2
            }
            return dummy.next
        };
        console.log(mergeTwoLists(l1,l2))
    
    </script>
</body>
</html>